#!/bin/bash
# Create all items' CPS link
#

if [ $# -lt 3 ]; then
        echo "ERROR Insuffcient arguments. Need 3 but got '$#'"
        echo "Usage: ./cpsfactory.sh </path/to/taobaoImporter/root/directory> </path/to/input/data/directory> </path/to/output/data/directory>"
        echo ""
        exit 1
fi

rootDir=$1; shift
inputdataDir=$1; shift
outputdataDir=$1; shift

# verify the existence of the directories
if [ ! -e "${rootDir}" -o ! -e "${inputdataDir}" -o ! -e "${outputdataDir}" ]; then
        echo "ERROR lib dir '${rootDir}' or/and input data dir '${inputdataDir}' or/and output data dir '${outputdataDir}' don't exist. Exit"
        echo "Usage: ./cpsfactory.sh </path/to/taobaoImporter/root/directory> </path/to/input/data/directory> </path/to/output/data/directory>"
        echo ""
        exit 1
fi

# trim the tailing '/' character
rootDir=`echo $rootDir | sed -e 's/\/$//'`

# trim the tailing '/' character if exists
inputdataDir=`echo $inputdataDir | sed -e 's/\/$//'`

# trim the tailing '/' character if exists and add one '/' character
outputdataDir=`echo $outputdataDir | sed -e 's/\/$//'`
outputdataDir=${outputdataDir}/
# construct the classpath
classpath=${rootDir}/release/taobao-importer-1.0.0.jar:${rootDir}/lib/commons-cli-1.2.jar:${rootDir}/lib/gson-1.7.1.jar:${rootDir}/lib/mongo-2.6.5.jar:${rootDir}/lib/taobao-sdk-j
ava-20110804.jar:${rootDir}/lib/commons-lang3-3.0.1.jar:${rootDir}/lib/log4j-1.2.16.jar

# specify the main function
mainClass=com.googlecode.ulysses.taobao.utilities.ProductIDToCPSLink
#mainClss=com.googlecode.ulysses.taobao.synchronization.DBSynchronization
# import
for i in `ls ${inputdataDir}/v*`
do
        importFile=$i

        echo 
        echo Start importing file $i

        echo  java -cp ${classpath} ${mainClass}  -f ${i} -k 12394200 -s d4eae10f30b02abbe8cc6c1ff42f40c1 -u 29445300 -o ${outputdataDir}
        java -cp ${classpath} ${mainClass}  -f ${i} -k 12394200 -s d4eae10f30b02abbe8cc6c1ff42f40c1 -u 29445300 #-o ${outputdataDir}

	echo Done.
	echo ""
done
